package com.wxapp.common.core.annotation.util;
import jakarta.servlet.http.HttpServletRequest;

/**
 * @author lgl
 */
public class IpUtil {
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        String key = "unknown";
        if (ip == null || ip.isEmpty() || key.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || key.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.isEmpty() || key.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.isEmpty() || key.equalsIgnoreCase(ip)) {
            ip = request.getRemoteHost();
        }
        if (ip !=null && !"".equals(ip) && ip.indexOf(",") > 0) {
            ip = ip.substring(0, ip.indexOf(","));
        }
        return ip.trim();
    }
    private IpUtil(){}
}
